package com.tanhua.config.template;

import com.baidu.aip.face.AipFace;
import com.tanhua.config.properties.Faceproperties;
import org.json.JSONObject;

import java.util.HashMap;

public class FaceTemeplate {
    private Faceproperties face;

    public FaceTemeplate(Faceproperties face) {
        this.face = face;
    }


    public Boolean Face(String imagUrl) {
        // 初始化一个AipFace
        AipFace client = new AipFace(face.getAppId(), face.getApiKey(), face.getSecretKey());

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 调用接口
        String image = imagUrl;
        String imageType = "URL";

        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");
        // 人脸检测
        JSONObject res = client.detect(image, imageType, options);
        System.out.println(res.toString(2));
        Integer error_code = (Integer) res.get("error_code");
        return error_code==0;
    }
}
